package com.ie.controller;

import com.ie.common.http.Result;
import com.ie.common.http.ResultCodeEnum;
import com.ie.entity.Admins;
import com.ie.entity.Bells;
import com.ie.entity.Users;
import com.ie.service.AdminsService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/system/admins")
public class AdminsController {
    @Autowired
    private AdminsService adminsService;

    //查询所有用户
    @GetMapping("/all")
    public Result getAllUser() {
        return Result.build(adminsService.getAllUser(), 1, "查询成功");
    }

    //删除用户
    @DeleteMapping("/delete")
    public Result deleteUser(@RequestParam("id") String id) {
        if (adminsService.deleteUser(id)) {
            return Result.build(null, 1, "删除成功");
        }

        return Result.build(null, 0, "删除失败");
    }

    //查询管理员
    @GetMapping("/search")
    public Result searchAdmins() {
        List<Admins> admins = adminsService.searchAdmins();
        if (admins != null) {
            return Result.build(admins, 1, "查询成功");
        }
        return Result.build(null, 0, "查询失败");
    }

    //修改管理员信息
    @PutMapping("/update")
    public Result updateAdmins(@RequestBody Admins admins) {
        if (adminsService.updateAdmins(admins)) {
            return Result.build(null, 1, "修改成功");
        }
        return Result.build(null, 0, "修改失败");
    }

    //删除管理员
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestParam("id") String id) {
        if (adminsService.deleteAdmin(id)) {
            return Result.build(null, 1, "删除成功");
        }
        return Result.build(null, 0, "删除失败");
    }

    //查询所有宠物
    @GetMapping("/allPets")
    public Result getAllPets() {
        return Result.build(adminsService.getAllPets(), 1, "查询成功");
    }

    //查询所有公告
    @GetMapping("/allMessages")
    public Result getAllMessages() {
        return Result.build(adminsService.getAllMessages(), 1, "查询成功");
    }

    //修改公告信息
    @PutMapping("/updateMessage")
    public Result updateMessage(@RequestBody Bells bells) {
        System.out.println(bells);

        if (adminsService.updateMessage(bells)) {
            return Result.build(null, 1, "修改成功");
        }
        return Result.build(null, 0, "修改失败");

    }

    //删除公告信息
    @DeleteMapping("/deleteMessage")
    public Result deleteMessage(@RequestParam("id") String id) {
        if (adminsService.deleteMessage(id)) {
            return Result.build(null, 1, "删除成功");
        }
        return Result.build(null, 0, "删除失败");
    }

    //添加管理员
    @PostMapping("/add")
    public Result addAdmins(@RequestBody Admins admins) {
        if (adminsService.addAdmins(admins)) {
            return Result.build(null, 1, "添加成功");
        }
        return Result.build(null, 0, "添加失败");
    }


    //模糊普配用户
    @GetMapping("/searchUser")
    public Result searchUser(@RequestParam("username") String username) {
        List<Users> admins = adminsService.searchUser(username);
        System.err.println(admins);
        if (admins != null) {
            return Result.build(admins, 1, "查询成功");
        }
        return Result.build(null, 0, "查询失败");
    }
}
